package com.lingchen.boot.utils;

import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.util.ArrayList;
import java.util.List;

/**
 * I'm a cute code dog
 * User: LingChen
 * Date:2017/10/10
 * Time:11:03
 */
public class ImageUtils {
    public static BufferedImage reline(BufferedImage srcImg,int px) {
        if (srcImg != null) {
            int width = srcImg.getWidth();
            int height = srcImg.getHeight();

            for (int x = 0; x < width; x++) {
                List<Integer> list = new ArrayList<Integer>();
                for (int y = 0; y < height; y++) {
                    int count = 0;
                    while (y < height - 1 && isBlack(srcImg.getRGB(x, y))) {
                        count++;
                        y++;
                    }
                    if (count <= px && count > 0) {
                        for (int i = 0; i <= count; i++) {
                            list.add(y - i);
                        }
                    }
                }
                if (list.size() != 0) {
                    for (int i = 0; i < list.size(); i++) {
                        srcImg.setRGB(x, list.get(i), Color.white.getRGB());
                    }
                }
            }
        }

   /*     int iw = srcImg.getWidth();
        int ih = srcImg.getHeight();
        Graphics2D srcG = srcImg.createGraphics();
        RenderingHints rhs = srcG.getRenderingHints();

        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorConvertOp theOp = new ColorConvertOp(cs, rhs);
        BufferedImage dstImg = new BufferedImage(iw, ih,
                BufferedImage.TYPE_INT_RGB);

        theOp.filter(srcImg, dstImg);
*/
        return srcImg;
    }


    public static boolean isBlack(int rgb){
        Color c = new Color(rgb);
        int b = c.getBlue();
        int r = c.getRed();
        int g = c.getGreen();
        int sum = r+g+b;
        if(sum<10){
            return true;
        }
        return false;
    }



}
